<template>
	<view class="affiliate-application">
		<!-- 表单内容 -->
		<view class="form-content">
			<!-- 输入字段 -->
			<view class="input-section">
				<view class="input-field">
					<input class="input" placeholder="手机号码" type="number" v-model="formData.phone" />
				</view>
				<view class="input-field">
					<input class="input" placeholder="真实姓名" type="text" v-model="formData.realName" />
				</view>
				<view class="input-field">
					<input class="input" placeholder="身份证号码" type="text" v-model="formData.idNumber" />
				</view>
			</view>

			<!-- 图片上传区域 -->
			<view class="upload-section">
				<view class="upload-item">
					<view class="upload-title">
						<text>身份证人像面</text>
					</view>
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" :maxCount="1">
						<image src="/static/affiliateApplication/idCardFace.png" mode="widthFix"
							style="width: 340px;height: 150px;"></image>
					</u-upload>
				</view>

				<view class="upload-item">
					<view class="upload-title">
						<text>身份证国徽面</text>
					</view>
					<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" :maxCount="1">
						<image src="/static/affiliateApplication/idCardCountry.png" mode="widthFix"
							style="width: 340px;height: 150px;"></image>
					</u-upload>
				</view>

				<view class="upload-item">
					<view class="upload-title">
						<text>手持身份证照片</text>
					</view>
					<u-upload :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" :maxCount="1">
						<image src="/static/affiliateApplication/idCardWithHuman.png" mode="widthFix"
							style="width: 340px;height: 150px;"></image>
					</u-upload>
				</view>
			</view>
		</view>

		<!-- 提交按钮 -->
		<view class="submit-section">
			<button class="submit-button" @click="submitApplication">提交申请</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					phone: '',
					realName: '',
					idNumber: ''
				},
				fileList1: [],
				fileList2: [],
				fileList3: []
			};
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "http://192.168.2.21:7001/upload", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data);
							}, 1000);
						},
					});
				});
			},
			submitApplication() {
				// 这里可以添加表单提交逻辑
				console.log('提交申请:', this.formData);
				console.log('身份证人像面:', this.fileList1);
				console.log('身份证国徽面:', this.fileList2);
				console.log('手持身份证照片:', this.fileList3);
			}
		}
	}
</script>

<style lang="scss">
	.affiliate-application {
		background-color: #fff;
		min-height: 100vh;
	}

	.nav-bar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 12px 16px;
		background-color: #fff;
		border-bottom: 1px solid #f0f0f0;
		position: sticky;
		top: 0;
		z-index: 100;

		.nav-left {
			.nav-icon {
				font-size: 20px;
				color: #333;
				font-weight: bold;
			}
		}

		.nav-title {
			font-size: 16px;
			color: #333;
			font-weight: bold;
		}

		.nav-right {
			display: flex;
			gap: 12px;

			.nav-icon {
				font-size: 16px;
				color: #333;
			}
		}
	}

	.form-content {
		padding: 20px 16px;
		padding-bottom: 100px; // 为底部按钮留出空间
	}

	.input-section {
		margin-bottom: 30px;
	}

	.input-field {
		margin-bottom: 16px;

		.input {
			width: 100%;
			height: 48px;
			background-color: #f8f8f8;
			border: 1px solid #e0e0e0;
			border-radius: 8px;
			padding: 0 16px;
			font-size: 16px;
			color: #333;
			box-sizing: border-box;

			&::placeholder {
				color: #999;
			}
		}
	}

	.upload-section {
		display: flex;
		flex-direction: column;
		gap: 20px;
	}

	.upload-item {


		.upload-title {
			margin-bottom: 12px;

			text {
				font-size: 14px;
				color: #333;
				font-weight: bold;
			}
		}
	}

	.submit-section {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 16px;
		background-color: #fff;
		border-top: 1px solid #f0f0f0;
		z-index: 99;

		.submit-button {
			width: 100%;
			background-color: #ff4757;
			color: #fff;
			border: none;
			border-radius: 8px;
			padding: 16px;
			font-size: 16px;
			font-weight: bold;
			line-height: 1.2;
		}
	}
</style>